package com.ruoyi.pos.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.pos.domain.Goods;
import com.ruoyi.pos.mapper.MaterialMapper;
import com.ruoyi.pos.domain.Material;
import com.ruoyi.pos.service.IMaterialService;

/**
 * posService业务层处理
 * 
 * @author wzh
 * @date 2021-04-19
 */
@Service
public class MaterialServiceImpl implements IMaterialService 
{
    @Autowired
    private MaterialMapper materialMapper;

    /**
     * 查询pos
     * 
     * @param id posID
     * @return pos
     */
    @Override
    public Material selectMaterialById(Long id)
    {
        return materialMapper.selectMaterialById(id);
    }

    /**
     * 查询pos列表
     * 
     * @param material pos
     * @return pos
     */
    @Override
    public List<Material> selectMaterialList(Material material)
    {
        return materialMapper.selectMaterialList(material);
    }

    /**
     * 新增pos
     * 
     * @param material pos
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMaterial(Material material)
    {
        int rows = materialMapper.insertMaterial(material);
        insertGoods(material);
        return rows;
    }

    /**
     * 修改pos
     * 
     * @param material pos
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMaterial(Material material)
    {
        materialMapper.deleteGoodsByMid(material.getId());
        insertGoods(material);
        return materialMapper.updateMaterial(material);
    }

    /**
     * 批量删除pos
     * 
     * @param ids 需要删除的posID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMaterialByIds(Long[] ids)
    {
        materialMapper.deleteGoodsByMids(ids);
        return materialMapper.deleteMaterialByIds(ids);
    }

    /**
     * 删除pos信息
     * 
     * @param id posID
     * @return 结果
     */
    @Override
    public int deleteMaterialById(Long id)
    {
        materialMapper.deleteGoodsByMid(id);
        return materialMapper.deleteMaterialById(id);
    }

    /**
     * 新增goods信息
     * 
     * @param material pos对象
     */
    public void insertGoods(Material material)
    {
        List<Goods> goodsList = material.getGoodsList();
        Long id = material.getId();
        if (StringUtils.isNotNull(goodsList))
        {
            List<Goods> list = new ArrayList<Goods>();
            for (Goods goods : goodsList)
            {
                goods.setMid(id);
                list.add(goods);
            }
            if (list.size() > 0)
            {
                materialMapper.batchGoods(list);
            }
        }
    }
}
